package com.njbdqn.ALi.zjg.Controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.njbdqn.ALi.zjg.entity.Comment;
import com.njbdqn.ALi.zjg.services.CommentService;
import com.njbdqn.ALi.zjg.services.TicketService;

/**
 * Servlet implementation class SaveSubcommentController
 */
public class SaveSubcommentController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		/*HttpSession session = request.getSession();
		Userinfos user = null;
		user = (Userinfos)session.getAttribute("user");*/
		
		String book_id = request.getParameter("book_id");
		String replyid = request.getParameter("replyid");
		String commentcontent = request.getParameter("commentcontent");
	
		
		int bid = Integer.parseInt(book_id);
		int rid = Integer.parseInt(replyid);
		
		
		Comment cm = new Comment();
		cm.setAccount(1);
		cm.setBook_id(bid);
		cm.setReplyid(rid);
		cm.setCommentcontent(commentcontent);
		cm.setGreat(0);
		cm.setCommentnumber(0);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		try {
			cm.setCommenttime(sdf.format(new Date()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int  count = new CommentService().save(cm);
		
		
		if(count>0){
			TicketService ts = new TicketService();
			request.setAttribute("userid", ts.modify(1));
			
			request.getRequestDispatcher("ticket").forward(request, response);
		}
	}

}
